package com.meizitu;

import com.meizitu.entity.Image;
import com.meizitu.entity.LinkSet;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

/**
 * 爬虫 妹子图
 *
 * @author zhouinfo
 * @Create Date 2016-07-31 14:51
 */
public class Spider {

    private String savaPath;

    private static LinkSet linkSet = new LinkSet();
    static int i=0;
    /**
     * 开始抓取图片
     */
    public static void begin() {
        spiderURL(Main.ROOTURL);
        spiderImage();
    }

    /**
     * 获取页面的
     */
    public static void spiderURL(String urlStr) {
        System.out.println(urlStr);
        URL url = null;
        try {
            url = new URL(urlStr);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        String HTML = getHTML(url);
        PatternMatcher.getPageHTML(HTML,linkSet);
        urlStr=PatternMatcher.getNextURL(HTML);
        if (urlStr!=null &&urlStr!=""){
            i++;
            spiderURL(urlStr);
        }
    }

    /**
     * 从linkSet中取出图片页面url 获取html源码 提取图片url 调用下载方法
     */
    public static void spiderImage() {
        Spider spider = new Spider();
        while (!linkSet.isEmptyUnVisitURL()) {
            String HTML = null;
            try {
                URL url = new URL(linkSet.getUnVisitURL());
                HTML = getHTML(url);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
            List<Image> imageList = PatternMatcher.getImageUrl(HTML);
            for (Image image : imageList) {
                System.out.println(image.getUrl());
            }
            //DownloadImage.downloadImageList(imageList);
        }

    }

    /**
     * 获取url的html源码
     * @param url
     * @return
     */
    public static String getHTML(URL url) {
        BufferedReader br = null;
        final StringBuffer sb = new StringBuffer();
        try {
            br = new BufferedReader(new InputStreamReader(url.openStream(), Main.ECODING));
            String str = "";
            while (null != (str = br.readLine())) {
                sb.append(str).append("\r\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

}
